package com.chaofan.authsso.client.common;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.io.Serializable;

/**
 * 公共返回体
 * Author: Chaofan
 * Date: 2020/6/4 14:24
 * email: chaofan2685@qq.com
 */
public class SsoRestfulMessage<T> implements Serializable {

    private static final long serialVersionUID = -1244146921919533755L;
    private Integer code;
    private String message;
    private T data;

    public SsoRestfulMessage() {
    }

    public SsoRestfulMessage(String json, Class<T> type) {
        JSONObject jsonObject = JSONUtil.parseObj(json);
        this.code = jsonObject.getInt("code");
        this.message = jsonObject.getStr("message");
        this.data = jsonObject.get("data",type);;
    }

    public SsoRestfulMessage(String json) {
        JSONObject jsonObject = JSONUtil.parseObj(json);
        this.code = jsonObject.getInt("code");
        this.message = jsonObject.getStr("message");
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public boolean successful(){
        return Constant.SUCCESS.intValue()==code.intValue();
    }
}
